//
//  XJAddressFormat.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/22.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  控制器瘦身 负责网络请求 业务逻辑

#import "XJAddressFormat.h"
#import "XJAddressModel.h"

@interface XJAddressFormat()
@property(nonatomic, strong) NSMutableArray *addressListArray;

@end
@implementation XJAddressFormat

- (NSMutableArray *)addressListArray {
    if (!_addressListArray) {
        _addressListArray = [NSMutableArray array];
    }
    return _addressListArray;
}

#pragma mark - 一 接口

- (void)requestAddresList {
    [XJHttpTool requestAddressListSuccessH:^(id responseObjc) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObjc options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        XJLog(@"收货地址列表::%@",jsonStr);
        
        NSNumber *code = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
           NSArray *listArr = responseObjc[@"data"][@"address_list"];
            
            [self rb_jsonFromArr:listArr];
            
            for (NSDictionary *dic in listArr) {
                XJAddressModel *model = [XJAddressModel mj_objectWithKeyValues:dic];
                
                [self.addressListArray addObject:model];
            }
            
            // 将数据传递出去 剩下的事交给代理去做
            if ([self.delegate respondsToSelector:@selector(addressFormatRequestAddressListDidSuccessWithArray:)]) {
                [self.delegate addressFormatRequestAddressListDidSuccessWithArray:self.addressListArray];
            }
        }
    } failureH:^(NSError *error) {
    
        if ([self.delegate respondsToSelector:@selector(addressFormatRequestAddressListFailed:)]) {
            [self.delegate addressFormatRequestAddressListFailed:error];
        }
    }];
}

- (void)selectAddressAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected {

    XJAddressModel *selectedModel = self.addressListArray[indexPath.row];
    
    if (selectedModel.isDefault == NO) {
        for (XJAddressModel *model in self.addressListArray) {
            model.isDefault = NO;
        }
    }
    
    selectedModel.isDefault = !selectedModel.isDefault;
    
    // 作用:刷新表格
    if ([self.delegate respondsToSelector:@selector(selectAddressAndReloadData)]) {
        [self.delegate selectAddressAndReloadData];
    }
    
    // 调接口设置默认地址
    XJLog(@"是否选中:%d",selectedModel.isDefault);
    if (selectedModel.isDefault == YES) {
        
        [XJHttpTool requestDefaultAddressWithDistrictId:selectedModel.addressId successH:^(id responseObjc) {
            
            NSNumber *code = responseObjc[@"code"];
            if ([code isEqualToNumber:@0]) {
                [JRToast showWithText:@"设置成功"];
                if ([self.delegate respondsToSelector:@selector(selectAddressAndReloadData)]) {
                    [self.delegate selectAddressAndReloadData];
                }
            }
        } fialH:^(NSError *error) {

        }];
    }
}


- (void)deleteAddressAtIndexPath:(NSIndexPath *)indexPath {
    XJAddressModel *currentModel = self.addressListArray[indexPath.row];
    
    // 调"删除地址"接口
    [XJHttpTool requestDeleteAddressWithDistrictId:currentModel.addressId successH:^(id responseObjc) {
        
        XJLog(@"删除成功:%@",responseObjc);
        NSNumber *code = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
            [JRToast showWithText:@"删除成功"];
            
            if ([self.delegate respondsToSelector:@selector(selectAddressAndReloadData)]) {
                [self.delegate selectAddressAndReloadData];
            }
        }
    } failH:^(NSError *error) {
        XJLog(@"删除失败:%@",error);
    }];
}


#pragma mark - 二 Private methods
- (void)rb_jsonFromArr:(NSArray *)dataArry {
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataArry options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    XJLog(@"地址列表:%@",jsonStr);
}
@end
